package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.MessageListBean;
import com.csweimei.rentmanagementtreasure.http.entity.TabThreeBean;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class MessInfoVM extends TitleVM {

    public ObservableField<TabThreeBean.DataDTO> entity = new ObservableField<>();

    public MessInfoVM(@NonNull Application application, BaseModel model) {
        super(application, model);
    }

    public ObservableList<MessageListItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<MessageListItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_mess_info);

    public ObservableField<Integer> showNo = new ObservableField<>(View.VISIBLE);
    public void getData(String type){
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("messageType",type);
        model.messagelist(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<MessageListBean>() {
                    @Override
                    public void onNext(MessageListBean o) {
                        if (o.issucc){
                            observableList.clear();
                            if (o.getData().size() > 0){
                                showNo.set(View.GONE);
                                for (MessageListBean.DataDTO bean : o.getData()) {
                                    MessageListItemVM messageListItemVM = new MessageListItemVM(MessInfoVM.this);
                                    messageListItemVM.setData(bean);
                                    observableList.add(messageListItemVM);
                                }
                            }else {
                                showNo.set(View.VISIBLE);
                            }

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNo.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
